<template>
  <div class="app-container">
    <div class="title">已审核</div>

    <div class="only">
      <div class="first">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="发布内容" prop="mes">
            <el-input v-model="queryParams.mes" placeholder="发布内容" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="发布人" prop="mesName">
            <el-input v-model="queryParams.mesName" placeholder="发布人" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['carService:message:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </div>

      <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标题" align="center" prop="mesTitle" />
        <el-table-column label="发布内容" align="center" prop="mes" />
        <el-table-column label="发布人" align="center" prop="mesName" />
        <el-table-column label="发布时间" align="center" prop="mesTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.mesTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="mesAudit">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.mesState === 0" size="small" type="success">审核通过</el-tag>
            <el-tag v-else-if="scope.row.mesState === 1" size="small" type="danger">审核不通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="getList(scope.row)"
              v-hasPermi="['carService:message:edit']">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />

      <!-- 添加或修改信息发布对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="发布标题" prop="mesTitle">
            <el-input v-model="form.mesTitle" placeholder="请输入发布标题" />
          </el-form-item>
          <el-form-item label="发布内容" prop="mes">
            <el-input type="textarea" v-model="form.mes"></el-input>
          </el-form-item>
          <el-form-item label="发布图片" prop="mesImg">
            <!--          <el-input v-model="form.mesImg" placeholder="请输入发布图片" />-->
            <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card"
              v-model="form.mesImg" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">发布</el-button>
          <el-button @click="cancel">取消</el-button>

        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listMessage, getNewMessage, delMessage, addMessage, updateMessage, updateState, newListMessage } from "@/api/carService/message";

export default {
  name: "shenhe",
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息发布表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      //
      mesState1: null,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mes: null,
        mesName: null,
        mesTime: null,
        mesState: null,
        mesImg: null,
        mesAudit: null,
        auditIllustrate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    toggleSaleStatus(row) {
      this.mesState1 = row.mesState;
      updateState(this.mesState1).then(response => {
        this.getList();
      })
    },
    /** 查询信息发布列表 */
    getList() {
      this.loading = true;
      newListMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mesId: null,
        mes: null,
        mesName: null,
        mesTime: null,
        mesState: null,
        mesImg: null,
        mesTitle: null,
        mesAudit: null,
        auditIllustrate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mesId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('carService/message/exportSuccess', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.title {
  font-weight: 600;
  margin: 20px 0;
}

.only {
  margin: 0 20px;
}

.first {
  padding: 10px 40px;
  background-color: #f5f5f5;
  margin-bottom: 10px;
}
</style>